package com.ybjzo2o.customer.utils;

import com.ybjzo2o.customer.model.domain.CurrentUser;
import com.ybjzo2o.customer.model.domain.TokenInfo;

public class TokenUtils {
    private static final String TOKEN_FORMAT = "%s:%s";

    public TokenUtils() {
    }

    public static String generateToken(String appId, String signType, String currentUserId, String currentUserNickName, String avatar, Long validityTime, String accessKeyId, String accessKeySecret) {
        TokenInfo tokenInfo = new TokenInfo();
        CurrentUser currentUser = new CurrentUser();
        currentUser.setId(currentUserId);
        currentUser.setNickName(currentUserNickName);
        currentUser.setUserAvatar(avatar);
        tokenInfo.setSignType(signType);
        tokenInfo.setAppId(appId);
        return generateToken(tokenInfo, validityTime, accessKeyId, accessKeySecret);
    }

    public static String generateToken(TokenInfo tokenInfo, Long validityTime, String accessKeyId, String accessKeySecret) {
        if (tokenInfo == null) {
            tokenInfo = new TokenInfo();
        }

        tokenInfo.setExpireTime(System.currentTimeMillis() + validityTime);
        String accessKeySecretPart = RsaUtils.encrypt(tokenInfo, accessKeySecret);
        String accessKeyIdPart = Base64.encode(accessKeyId);
        return String.format("%s:%s", accessKeyIdPart, accessKeySecretPart);
    }

    public static String generateToken(String appId, String signType, Long validityTime, String accessKeyId, String accessKeySecret) {
        TokenInfo tokenInfo = new TokenInfo();
        tokenInfo.setAppId(appId);
        tokenInfo.setSignType(signType);
        return generateToken(tokenInfo, validityTime, accessKeyId, accessKeySecret);
    }
}